package com.colin.controller;

import com.colin.pojo.GuestList;
import com.colin.pojo.Users;
import com.colin.service.GuestServiceImpl;
import com.colin.service.UsersServiceImpl;
import com.colin.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("/member")
public class MemberController {
    @Autowired
    private GuestServiceImpl guestService;
    @Autowired
    private UsersServiceImpl usersService;

    // 获取用户信息
    protected Users getUserInfo(HttpSession session){
        int uid = getUid(session);
        return usersService.queryUserInfo(uid);
    }

    // 获取用户ID
    protected int getUid(HttpSession session){
        return (int) session.getAttribute("userInfoId");
    }

    @RequestMapping("/index")
    public String index(Model model , HttpSession session){
        model.addAttribute("user" , getUserInfo(session));
        // 我的留言
        HashMap<Object, Object> map = new HashMap<>();
        map.put("uid" , session.getAttribute("userInfoId"));
        List<GuestList> guestLists = guestService.queryAllGuest(map);
        model.addAttribute("guestList" , guestLists);
        return "member/index";
    }

    @RequestMapping("/profile")
    public String profile(HttpSession session , Model model){
        Users users = getUserInfo(session);
        model.addAttribute("user" , users);
        users.setSex(users.getSex() - 1);
        model.addAttribute("userSkills" , StringUtils.parse(users.getSkill() , ","));
        System.out.println(users);
        model.addAttribute("skills" , usersService.getSkills());
        model.addAttribute("sex" , usersService.getSex());
        model.addAttribute("userInfo" , users);
        return "member/profile";
    }

    @PostMapping("/profile")
    public String doProfile(@RequestParam("name") String name , String sex , @RequestParam(value = "avatar",required = false) CommonsMultipartFile avatar , @RequestParam(value = "skill[]",required = false) ArrayList<Object> skill , HttpServletRequest request){
        Users users = new Users();
        // 上传路径保存设置
        if (!avatar.isEmpty()){
            String path = request.getServletContext().getRealPath("/upload");
            File realPath = new File(path);
            if (!realPath.exists()){
                realPath.mkdir();
            }
            // 上传文件地址
            path = realPath + "/" + avatar.getOriginalFilename();
            try {
                avatar.transferTo(new File(path));
                users.setAvatar("/upload/" + avatar.getOriginalFilename());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        users.setId(getUid(request.getSession()));
        users.setName(name);
        users.setSex(Integer.parseInt(sex) + 1);
        users.setSkill(StringUtils.join(skill, ","));
        usersService.updateUser(users);
        return "redirect:/member/profile";
    }

    @RequestMapping("/updatePass")
    public String updatePass(HttpSession session , Model model){
        model.addAttribute("user" , getUserInfo(session));
        return "member/updatePass";
    }

    @PostMapping("/updatePass")
    public String doUpdatePass(String oldPass , String newPass , String confirmPass , HttpSession session , Model model){
        if (oldPass == null){
            model.addAttribute("error" , "原密码不能为空");
            return "message";
        }
        if (newPass == null){
            model.addAttribute("error" , "新密码不能为空");
            return "message";
        }
        if (confirmPass == null){
            model.addAttribute("error" , "确认密码不能为空");
            return "message";
        }
        if (!newPass.equals(confirmPass)){
            model.addAttribute("error" , "两次密码输入不一致");
            return "message";
        }
        // 检查用户密码是否正确
        Users users = usersService.queryUserInfo(getUid(session));
        if (!users.getPass().equals(oldPass)) {
            model.addAttribute("error" , "原密码不正确");
            return "message";
        }
        Users userModel = new Users();
        userModel.setId(getUid(session));
        userModel.setPass(confirmPass);
        usersService.updateUserPass(userModel);
        return "redirect:/public/logout";
    }
}
